package kr.ac.chungbuk;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	IBoardDAO ibaties = new IBoardDAO();
	List<BoardDTO> boardList = new ArrayList<BoardDTO>();
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "home";
	}
	@RequestMapping("test")
	public String test(){
		return "test";
	}
	@RequestMapping("TopMain")
	public String TopMain(){
		return "TopMain";
	}
	@RequestMapping("menu")
	public String menu(){
		return "../views/Menu/menu";
	}
	@RequestMapping("chicken")
	public String chicken(){
		return "../views/Menu/chicken";
	}
	@RequestMapping("sub_menu")
	public String sub_menu(){
		return "../views/Menu/sub_menu";
	}
	@RequestMapping("footer")
	public String footer(){
		return "footer";
	}
	@RequestMapping("login")
	public String login(){
		return "login";
	}
	@RequestMapping("colpop")
	public String colpop(){
		return "../views/Menu/colpop";
	}
	@RequestMapping("etc")
	public String etc(){
		return "../views/Menu/etc";
	}
	@RequestMapping("store")
	public String store(){
		return "../views/store/store";
	}
	@RequestMapping(value="loginProcess",method=RequestMethod.POST)
	public ModelAndView login(ModelAndView mav,String id, String pw,HttpServletRequest request){
		MemberDTO member=ibaties.selectOne(id);
		
		if(member==null){
			System.out.println("사용자 아이디값이 없습니다.");
			mav.setViewName("redirect:index");
		}else{
			System.out.println("id사용자아이디는"+member.getId());
				
			if(pw.equals(member.getPw())){
				request.getSession().setAttribute("member",member);
			}else{
				System.out.println("패스워드가 틀립니다.");
			}
			mav.setViewName("redirect:index");
			}
		return mav;
	}
	@RequestMapping("news")
	public ModelAndView news(ModelAndView mav,HttpSession session){
		MemberDTO member=(MemberDTO)session.getAttribute("member");
		boardList=ibaties.selectall();
		mav.addObject("boardList",boardList);
		
		mav.setViewName("../views/news/newsmain");
		
		if(member==null){
			mav.addObject("loginpage","../login.jsp");
		}
		else{
			mav.addObject("loginpage","../loginSuccess.jsp");
		}
		mav.addObject("writepage","list.jsp");
		
		
		return mav;
	}
	@RequestMapping("newsmain")
	public String newsmain(){
		return "../views/news/main";
	}
	@RequestMapping("write")
	public String write(){
		return "../views/news/write";
	}
	
	@RequestMapping(value="writeProc",method=RequestMethod.GET)
	public ModelAndView writeProc(ModelAndView mav,HttpSession session,String title,String content){
		MemberDTO member=(MemberDTO)session.getAttribute("member");
		if(member==null){
			mav.setViewName("../views/news/news");
		}
		else{
			String id=member.getId();
			String mem_name=member.getMem_name();
			Timestamp update_time=getCurrentTime();
			BoardDTO board=new BoardDTO(id,mem_name,title,content,update_time);
			ibaties.insertBoard(board);
		}
		mav.setViewName("redirect:news");
		return mav;
	}
	private Timestamp getCurrentTime(){
		return new Timestamp(Calendar.getInstance().getTimeInMillis());
	}
	@RequestMapping("main")
	public String main(){
		return "main";
	}
	@RequestMapping("index")
	public ModelAndView index(ModelAndView mav,HttpServletRequest request){
			
		mav.setViewName("main");
		
		if(request.getSession().getAttribute("member")!=null){
			//로그인되었을때
			mav.addObject("loginpage","loginSuccess.jsp");
		}else{
			//사용자아이디없을때
			mav.addObject("loginpage","login.jsp");
		}
		
		
		return mav;
	
	}
	@RequestMapping("logout")
	public String logout(HttpServletRequest request){
		request.getSession().invalidate();
		return "redirect:index";
	}
	@RequestMapping("memberEntry")
	public String memberEntry(){
		return "memberEntry";
	}
	@RequestMapping("memberEntrySuccess")
	public String memberEntrySuccess(){
		return "memberEntrySuccess";
	}
	@RequestMapping(value="insertProc",method=RequestMethod.GET)
	public ModelAndView insertProc(ModelAndView mav,MemberDTO member){
		ibaties.insertMember(member);
		mav.addObject("member", member);
		mav.setViewName("memberEntrySuccess");
		
		return mav;
	}
	
	
}
